<?php
include('init.php');
$act = isset($_REQUEST['act'])?$_REQUEST['act']:'';
switch($act)
{
	case 'save':
		$db = AppMain::getDbDriver();
		$accountInfo = array();
		$accountInfo[DBC_ACCOUNT_ACTIVE] = $_REQUEST['status'];	
		$accountInfo[DBC_ACCOUNT_NAME] = $_REQUEST['account'];
		$accountInfo[DBC_ACCOUNT_EMAIL] = $_REQUEST['email'];
		$accountInfo[DBC_ACCOUNT_MULTIPLE] = intval($_REQUEST['multiple']);
		if(strlen($_REQUEST['password']) > 0)
		{
			$accountInfo[DBC_ACCOUNT_PASSWORD] = $_REQUEST['password'];
		}
		
		//check accountName takend
		$checkResult = true;
		$rows = $db->selectWhere(DB_TABLE_NAME_ACCOUNT, 
                                  new SimpleWhereClause(DBC_ACCOUNT_NAME, '=', trim($_REQUEST[account])));
		if(count($rows) >  0)
		{
			if($rows[0][DBC_ACCOUNT_ID] != $_REQUEST['accountid'])
			{
				$checkResult = false;
			}
		}
		if($checkResult === false)
		{
			$GLOBALS['errorMessage'] = "Account name has been taken by other, please choose another one";
			$_REQUEST['act'] = 'add';
			$info = $accountInfo;
		}else{
		if(intval($_REQUEST['accountid']) > 0)
		{		
			$db->updateSetWhere(DB_TABLE_NAME_ACCOUNT,$accountInfo, new SimpleWhereClause(DBC_ACCOUNT_ID, '=', $_REQUEST['accountid']));
		}else{
			$db->insertWithAutoId(DB_TABLE_NAME_ACCOUNT, DBC_ACCOUNT_ID, $accountInfo);
		}
		}
		$GLOBALS['successMessage'] = '修改成功';
	break;
	case 'edit':
		$db = AppMain::getDbDriver();
		$rows = $db->selectWhere(DB_TABLE_NAME_ACCOUNT, 
                                  new SimpleWhereClause(DBC_ACCOUNT_ID, '=', $_REQUEST['id']));
		if(count($rows) == 1)
			$info = $rows[0];
	break;
	case 'delete':
		$loginAccountID = SessionUtil::getVar(SESSION_KEY_LOGIN_ID);
		if(trim($_REQUEST['id']) == trim($loginAccountID))
		{
			$GLOBALS['errorMessage'] = "Can't delete your account.";
		}else{
		$db = AppMain::getDbDriver();
	    $db->deleteWhere(DB_TABLE_NAME_ACCOUNT, new AndWhereClause(
            new SimpleWhereClause(DBC_ACCOUNT_ID, '=', $_REQUEST['id'], STRING_COMPARISON)));
            }

	break;
}
//reload account info
global_load_account_info_to_request();
$account_list = $_REQUEST['CACHE_ACCOUNT_TABLE'];

?>
<?php include 'header.php' ?>
<?php include 'index_top.php' ?>

<script language="javascript">
function checkFormValue(form)
{
	if(form.password.value != '' && form.password.value != form.confirm.value)
	{
		alert('新密码和确认密码不同');
		return false;
	}
	form.submit();
	return false;
}
function deleteConfirm(accountid)
{
	if(confirm('Delete?'))
	{
		document.location = '?act=delete&id='+accountid;
	}
	return false;
}
$(function(){
	$("select[name=status] option[value='<?php echo $info[DBC_ACCOUNT_ACTIVE];?>']").attr("selected", true); 
});
</script>
<?php if(strlen($GLOBALS['successMessage']) > 0):?>
<div class="success_message">
	<?php echo $GLOBALS['successMessage'];?>
</div>
<?php endif;?>
<?php if(strlen($GLOBALS['errorMessage']) > 0):?>
<div class="error_message">
	<?php echo $GLOBALS['errorMessage'];?>
</div>
<?php endif;?>
<?php if($_REQUEST['act'] == 'add' || $_REQUEST['act'] == 'edit'):?>
<form method="post">
<input type="hidden" name="accountid" value="<?php echo $info[DBC_ACCOUNT_ID];?>"/>
<input type="hidden" name="act" value="save"/>
<div class="adddiv" style="margin-bottom:20px;">
	<label>状态</label>
	<select name="status">
		<option value="Y">启用</option>
		<option value="N">暂停</option>
	</select>
	<label>用户名</label>
	<input name="account" value="<?php echo $info[DBC_ACCOUNT_NAME];?>"/>
	<label>邮箱</label>
	<input name="email" value="<?php echo $info[DBC_ACCOUNT_EMAIL];?>"/>
	<label>权重</label>
	<input name="multiple" value="<?php echo $info[DBC_ACCOUNT_MULTIPLE];?>"/>
	<label>新密码</label>
	<input name="password" type="password"/>
	<label>确认密码</label>
	<input name="confirm" type="password"/>
	<label></label>
	<input type="button" value="OK" class="button" onclick="return checkFormValue(this.form);"/>
	<input style="margin-left:20px;" type="button" value="Cancel" class="button" onclick="document.location='account.php';"/>
	<input style="margin-left:120px;" type="button" value="Delete" class="button" onclick="return deleteConfirm('<?php echo $info[DBC_ACCOUNT_ID];?>');"/>
</div>
</form>
<?php endif;?>
<style>

	.weekdatatable{width:100%;}
	.weekdatatable li{padding:10px}
	a{color:#000000;text-decoration:none;width:100%;}
	
	ul:hover{background-color:#aabbcc;}
</style>
<div style="clear:both;padding-left:30px;">
	<input type="button" value="Add" class="button" onclick="document.location='?act=add';return false"/>
</div>
<div class="adddiv">
<ul class="weekdatatable">
	<li>ID</li>
	<li>用户名</li>
	<li style="width:200px;">邮箱</li>
	<li>余额</li>
	<li>权重</li>
	<li>状态</li>
</ul>
	<?php foreach($account_list as $index=>$accountData):?>
	<a href="?act=edit&id=<?php echo $accountData[DBC_ACCOUNT_ID];?>">
		<ul class="weekdatatable" style="clear:both;cursor:hand;">
		<li><?php echo $accountData[DBC_ACCOUNT_ID];?></li>
		<li><?php echo $accountData[DBC_ACCOUNT_NAME];?></li>
		<li style="width:200px;"><?php echo $accountData[DBC_ACCOUNT_EMAIL];?></li>
		<li><?php echo $accountData[DBC_ACCOUNT_BALANCE];?></li>
		<li><?php echo $accountData[DBC_ACCOUNT_MULTIPLE]?$accountData[DBC_ACCOUNT_MULTIPLE]:1;?></li>
		<li><?php echo $accountData[DBC_ACCOUNT_ACTIVE] === 'Y'?'Y':'N';?></li>
		</ul>
	</a>
	<?php endforeach;?>
</div>
<?php include 'footer.php' ?>